package com.shundr.user;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.common.CommonWebViewActivity;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.common.util.ToastHelper;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.user.http.UserRequest;
import com.shundr.user.model.CreditsDetailInfo;
import com.shundr.user.util.CreditsConfig;

/**
 * 邀请用户 InvitationActivity
 * 
 * @author larry 2015-6-26 下午12:54:54
 * @version 1.0.0
 * 
 */
public class CreditsActivity extends BaseActivity{
	
	private TextView tvPhone;
	private TextView tvIntegral;
	private TextView tvIntegralFreezing;
	
	private ImageView ivHead;
	
	private DisplayImageOptions mOptions;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case UserRequest.GET_INTEGRAL_DETAIL_SUCCESSED:
				CreditsDetailInfo info = (CreditsDetailInfo)msg.obj;
				tvIntegral.setText("可用 : " + info.getAvailableCredits());
				tvIntegralFreezing.setText("冻结 : " + info.getFreezingCredits());
				ProgressDialogHelper.dismiss();
				break;
			case LHttpRequest.REQUEST_FAILED:
				ProgressDialogHelper.dismiss();
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_credits);
		initOptions();
		initView();
		ProgressDialogHelper.show(mContext);
	}

	@Override
	protected void onResume() {
		new UserRequest(mContext, mHandler).getCreditsDetail();
		super.onResume();
	}

	private void initView() {
		tvPhone = (TextView)findViewById(R.id.tv_phone);
		tvIntegral = (TextView)findViewById(R.id.tv_integral);
		tvIntegralFreezing = (TextView)findViewById(R.id.tv_integral_freezing);
		ivHead = (ImageView)findViewById(R.id.iv_portrait);
		
		tvPhone.setText(LContext.userInfo.getUserPhone());
		String headUrl=LContext.userInfo.getUserHeadPic();
		ImageLoader.getInstance().displayImage(headUrl, ivHead,mOptions);
		
		((TextView)findViewById(R.id.btn_1)).setText(CreditsConfig.CREDITS_20);
		((TextView)findViewById(R.id.btn_2)).setText(CreditsConfig.CREDITS_30);
		((TextView)findViewById(R.id.btn_3)).setText(CreditsConfig.CREDITS_50);
		((TextView)findViewById(R.id.btn_4)).setText(CreditsConfig.CREDITS_100);

		findViewById(R.id.btn_earn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, CommonWebViewActivity.class);
				intent.putExtra("url", "http://www.shundr.com/about/index/integralrule.html");
				intent.putExtra("title", "赚积分");
				startActivity(intent);
			}
		});
		
		findViewById(R.id.btn_record).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(mContext, CreditsRecordActivity.class));
			}
		});
		
		findViewById(R.id.btn_exchange).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(mContext, CreditsExchangeActivity.class));
			}
		});
		
		findViewById(R.id.layout_20).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ExchangeDetailActivity.class);
				intent.putExtra("flag", 0);
				startActivity(intent);
			}
		});
		findViewById(R.id.layout_30).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ExchangeDetailActivity.class);
				intent.putExtra("flag", 1);
				startActivity(intent);
			}
		});
		findViewById(R.id.layout_50).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ExchangeDetailActivity.class);
				intent.putExtra("flag", 2);
				startActivity(intent);
			}
		});
		findViewById(R.id.layout_100).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ExchangeDetailActivity.class);
				intent.putExtra("flag", 3);
				startActivity(intent);
			}
		});
	}
	
	private void initOptions(){
		mOptions = new DisplayImageOptions.Builder()  
		.showImageForEmptyUri(R.drawable.iv_head)//设置图片Uri为空或是错误的时候显示的图片  
		.showImageOnFail(R.drawable.iv_head)  
		.cacheInMemory(true) //设置图片加载/解码过程中错误时候显示的图片  
		.cacheInMemory(true)//设置下载的图片是否缓存在内存中  
		.cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中  
		.considerExifParams(true)  
		.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示  
		.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//  
//		.decodingOptions(Options decodingOptions)//设置图片的解码配置  
		.considerExifParams(true)//设置图片下载前的延迟  
		//.delayBeforeLoading(int delayInMillis)//int delayInMillis为你设置的延迟时间  
		//设置图片加入缓存前，对bitmap进行设置  
		//.preProcessor(BitmapProcessor preProcessor)  
//		.resetViewBeforeLoading(true)//设置图片在下载前是否重置，复位  
		.displayer(new RoundedBitmapDisplayer(45))//是否设置为圆角，弧度为多少  
//		.displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间  
		.build();//构建完成  
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
